package com.hankol.material.respository

import androidx.lifecycle.MutableLiveData
import com.baselibrary.net.APIResponse
import com.baselibrary.net.MyHttpUtil
import com.hankol.material.api.MaterialApi
import com.hankol.material.bean.*
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

/**
 *  * @author zhanglijie
 * @description 搜索管理
 * @date : 2020/03/08
 */
class MaterialReqManager {
    companion object {
        @Volatile
        private var instance: MaterialReqManager? = null

        fun getInstance(): MaterialReqManager {
            return instance ?: synchronized(this) {
                instance ?: MaterialReqManager().also { instance = it }
            }
        }
    }

    /**
     *首页列表数据
     */
    fun getMaterialHome(
        hotList: MutableLiveData<MaterialHomeBean>,
        types: String,
        firstCid: Int,
        secondCids: String,
        page: Int
    ) {
        MyHttpUtil.getInstance().getApi(MaterialApi::class.java)
            .getMaterialHome(types, firstCid, secondCids, page).subscribeOn(Schedulers.newThread())
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : APIResponse<MaterialHomeBean>(this) {
                override fun onSuccess(result: MaterialHomeBean?, json: String?) {
                    hotList.value = result
                }
            })
    }

    /**
     *资料库分类
     */
    fun getMaterialCatalogueList(hotList: MutableLiveData<List<LevelOneBean>>, pid: Long) {
        MyHttpUtil.getInstance().getApi(MaterialApi::class.java).getCatalogueList(pid)
            .subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : APIResponse<List<LevelOneBean>>(this) {
                override fun onSuccess(result: List<LevelOneBean>?, json: String?) {
                    hotList.value = result
                }

                override fun onFailed(message: String?) {
                    //super.onFailed(message)
                    hotList.value = null
                }
            });
    }

    /**
     *新建资料夹
     */
    fun createFolder(hotList: MutableLiveData<CreateFolderBean>, folderName: String) {
        MyHttpUtil.getInstance().getApi(MaterialApi::class.java).createFolder(folderName)
            .subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : APIResponse<CreateFolderBean>(this) {
                override fun onSuccess(result: CreateFolderBean?, json: String?) {
                    hotList.value = result
                }

                override fun onFailed(message: String?) {
                    super.onFailed(message)
                    hotList.value = null
                }
            });
    }

    /**
     *新建资料夹
     */
    fun getFolderList(hotList: MutableLiveData<List<FolderListBean>>) {
        MyHttpUtil.getInstance().getApi(MaterialApi::class.java).getFolderList()
            .subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : APIResponse<List<FolderListBean>>(this) {
                override fun onSuccess(result: List<FolderListBean>?, json: String?) {
                    hotList.value = result
                }

                override fun onFailed(message: String?) {
                    //super.onFailed(message)
                }
            });
    }

    /**
     *增加文件到资料夹
     */
    fun addFileToFolder(fileId: Long, folderId: Long, apiResponse: APIResponse<CreateFolderBean>) {
        MyHttpUtil.getInstance().getApi(MaterialApi::class.java).addFileToFolder(fileId, folderId)
            .subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread())
            .subscribe(apiResponse);
    }

    /**
     *删除资料夹
     */
    fun deleteFolder(hotList: MutableLiveData<Boolean>, folderId: Long) {
        MyHttpUtil.getInstance().getApi(MaterialApi::class.java).deleteFolder(folderId)
            .subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : APIResponse<Boolean>(this) {
                override fun onSuccess(result: Boolean?, json: String?) {
                    hotList.value = result
                }

                override fun onFailed(message: String?) {
                    //super.onFailed(message)
                    hotList.value = false
                }
            });
    }

    /**
     *重命名资料夹
     */
    fun renameFolder(hotList: MutableLiveData<Boolean>, folderName: String, folderId: Long) {
        MyHttpUtil.getInstance().getApi(MaterialApi::class.java).renameFolder(folderName, folderId)
            .subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : APIResponse<Boolean>(this) {
                override fun onSuccess(result: Boolean?, json: String?) {
                    hotList.value = result
                }

                override fun onFailed(message: String?) {
                    super.onFailed(message)
                    hotList.value = false
                }
            });
    }

    /**
     *取消收藏
     */
    fun cancelCollection(cid: Long, fid: Long, apiResponse: APIResponse<Boolean>) {
        MyHttpUtil.getInstance().getApi(MaterialApi::class.java).cancelCollection(cid, fid)
            .subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread())
            .subscribe(apiResponse);
    }

    /**
     *转移文件
     */
    fun loadMoverFolder(fid: Long, newCid: Long, oldCid: Long, apiResponse: APIResponse<Boolean>) {
        MyHttpUtil.getInstance().getApi(MaterialApi::class.java)
            .loadMoverFolder(fid, newCid, oldCid)
            .subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread())
            .subscribe(apiResponse);
    }

    /**
     *素材库目录筛选
     */
    fun reqCatalogueList(
        ld: MutableLiveData<MaterialContentInfo>, categoryId: Long, page: Int, type: Int
    ) {
        MyHttpUtil.getInstance().getApi(MaterialApi::class.java)
            .reqCatalogueList(categoryId, page, type)
            .subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : APIResponse<MaterialContentInfo>(this) {
                override fun onSuccess(result: MaterialContentInfo?, json: String?) {
                    ld.value = result
                }

                override fun onFailed(message: String?) {
                    ld.value = null
                    //super.onFailed(message)
                }

            })
    }


    /**
     * 资料夹详情界面
     */
    fun reqFolderList(
        ld: MutableLiveData<MaterialContentInfo>, collectionId: Long, page: Int, type: Int
    ) {
        MyHttpUtil.getInstance().getApi(MaterialApi::class.java)
            .reqFolderList(collectionId, page, type)
            .subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : APIResponse<MaterialContentInfo>(this) {
                override fun onSuccess(result: MaterialContentInfo?, json: String?) {
                    ld.value = result
                }

                override fun onFailed(message: String?) {
                    ld.value = null
                    // super.onFailed(message)
                }

            })
    }

    /**
     * 搜索素材
     */
    fun reqSearch(
        ld: MutableLiveData<MaterialContentInfo>, searchContent: String, page: Int, type: Int
    ) {
        MyHttpUtil.getInstance().getApi(MaterialApi::class.java)
            .reqSearch(searchContent, page, type)
            .subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : APIResponse<MaterialContentInfo>(this) {
                override fun onSuccess(result: MaterialContentInfo?, json: String?) {
                    ld.value = result
                }

                override fun onFailed(message: String?) {
                    ld.value = null
                    //super.onFailed(message)
                }

            })
    }

    /**
     * 素材是否存在
     */
    fun reqMaterialAuthenticity(fid: Long, apiResponse: APIResponse<Boolean>) {
        MyHttpUtil.getInstance().getApi(MaterialApi::class.java)
            .reqMaterialAuthenticity(fid)
            .subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread())
            .subscribe(apiResponse)
    }

    /**
     * 增加分享/下载数量
     */
    fun reqAddShareCount(fid: Long, type: Int) {
        MyHttpUtil.getInstance().getApi(MaterialApi::class.java)
            .reqAddShareCount(fid, type)
            .subscribeOn(Schedulers.newThread()).observeOn(AndroidSchedulers.mainThread())
            .subscribe(object : APIResponse<Any>(this) {
                override fun onSuccess(result: Any?, json: String?) {
                }

                override fun onFailed(message: String?) {

                }
            })
    }


}